
//////// main.cpp ////////////////////////////////////////////////////////
//    Author:     Richard    (richard.lindholm85@gmail.com)             //
//    Project:    some game project                                     //
//    Purpose:                                                          //
//////////////////////////////////////////////////////////////////////////

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include "theGame.h"

LRESULT WINAPI wndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hpInstance, LPSTR lpcmdLine, INT ncmdShow)
{
    WNDCLASS wc;

    wc.style = CS_CLASSDC;
    wc.lpfnWndProc = wndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = 0;
    wc.hCursor = 0;
    wc.hbrBackground = 0;
    wc.lpszMenuName = 0;
    wc.lpszClassName = L"someGame";

    RegisterClass(&wc);

    HWND hWnd = CreateWindow(
                L"someGame",
                L"",
                WS_OVERLAPPED | WS_POPUPWINDOW | WS_SYSMENU,
                100, 100,
                600, 450,
                0,
                0,
                hInstance,
                0);

    if (!hWnd)
    {
        MessageBox(0, L"Could'nt create a window!", L"Error", MB_OK | MB_ICONWARNING);
        return -1;
    }

    
    // App init here
    cTheGame* theGame = new cTheGame();
    if (!theGame->initiateApp(hWnd, hInstance))
    {
        MessageBoxA(hWnd, "theGame->initiateApp() failed!\nThis program will now close.", "Error", MB_ICONWARNING | MB_OK);
        delete theGame;
        return 0;
    }
    
    if (lpcmdLine)
    {
        theGame->setCmdLineStr(lpcmdLine);
    }

    UpdateWindow(hWnd);
    ShowWindow(hWnd, SW_SHOW);

    ShowCursor( false );

    MSG msg;

    bool running = true;
    while (running)
    {
        while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
                running = false;

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        
        if (!running)
            break;

        // App tick here
        theGame->tick();

    }


    // App end here
    delete theGame;

    return 0;
}


LRESULT WINAPI wndProc( HWND hWnd, UINT msg, WPARAM wparam, LPARAM lparam )
{
    switch( msg )
    {
        case WM_DESTROY:
        {
            PostQuitMessage( 0 );
            break;
        }
    }
    
    return DefWindowProc( hWnd, msg, wparam, lparam );
}